<!DOCTYPE html>
<html>
<head>
    <!-- 添加视口元标签 -->
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>幼儿保健智能AI助手</title>
    <style>
        body {
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            font-family: Arial, sans-serif;
            /* 设置 body 高度为 100vh */
            min-height: 93vh;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .chat-container {
            border: 1px solid #ccc;
            border-radius: 8px;
            padding: 20px;
            /* 设置聊天容器高度为 90vh，留出一些空间 */
            height: 85vh;
            display: flex;
            flex-direction: column;
            width: 100%;
        }
        #chat-history {
            flex: 1;
            overflow-y: auto;
            margin-bottom: 15px;
            padding: 10px;
            background: #f9f9f9;
            border-radius: 4px;
        }
        .message {
            margin-bottom: 10px;
            padding: 8px;
            border-radius: 4px;
        }
        .user-message {
            background: #58bb58;
            margin-left: 20%;
        }
        .bot-message {
            background: #ded376;
            margin-right: 20%;
        }
        #input-area {
            display: flex;
            gap: 10px;
        }
        #message-input {
            flex: 1;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }
        button {
            padding: 10px 20px;
            background: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        /* 媒体查询，适配小屏幕设备 */
        @media (max-width: 600px) {
            body {
                padding: 10px;
            }
            .chat-container {
                height: 90vh;
            }
            .user-message {
                margin-left: 10%;
            }
            .bot-message {
                margin-right: 10%;
            }
        }
    </style>
</head>
<body>
    <div class="chat-container">
        <h2>幼儿保健智能AI助手</h2>
        <div id="chat-history">
            <!-- 对话记录将动态加载 -->
        </div>
        <div id="input-area">
            <input type="text" id="message-input" placeholder="输入您的消息..."  onkeypress="handleKeyPress(event)">
            <button onclick="sendMessage()">发送</button>
        </div>
    </div>

    <script>
        function appendMessage(message, isUser) {
            const history = document.getElementById('chat-history');
            const messageDiv = document.createElement('div');
            messageDiv.className = `message ${isUser ? 'user-message' : 'bot-message'}`;
            messageDiv.textContent = message;
            history.appendChild(messageDiv);
            history.scrollTop = history.scrollHeight;
        }

        function handleKeyPress(event) {
            if (event.keyCode === 13) {
                event.preventDefault();
                sendMessage();
            }
        }

        async function sendMessage() {
            const input = document.getElementById('message-input');
            const message = input.value.trim();
            if (!message) return;

            appendMessage(message, true);
            input.value = '';

            try {
                const response = await fetch('/chat', {
                    method: 'POST',
                    headers: {'Content-Type': 'application/json'},
                    body: JSON.stringify({ message })
                });

                if (response.ok) {
                    const data = await response.json();
                    appendMessage(data.response, false);
                } else {
                    appendMessage('请求失败，请稍后再试', false);
                }
            } catch (error) {
                console.error('Error:', error);
                appendMessage('网络连接异常', false);
            }
        }
    </script>
</body>
</html>